package com.oracle.controller;

import com.oracle.annotation.Action;
import com.oracle.annotation.ActionType;
import com.oracle.pojo.Permission;
import com.oracle.pojo.Roles;
import com.oracle.role.service.api.RoleServiceApi;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired(required = false)
    private RoleServiceApi roleServiceApi;

    @Action(ActionType.QUERY)
    @RequestMapping("/list")
    public String roleList(Model model){
        ResultEntity<List<Roles>> resultEntity = this.roleServiceApi.findRoleAll();
        model.addAttribute("roleList",resultEntity.getData());
        return "admin/role/list";
    }

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/addview")
    public String roleAddView(Model model){
        ResultEntity<List<Permission>> resultEntity = this.roleServiceApi.getPermissionsAll();
        model.addAttribute("perList",resultEntity.getData());
        return "admin/role/addview";
    }

    @Action(ActionType.ADD)
    @RequestMapping("/add")
    public String roleAdd(Roles roles,Integer...perids){
        //添加业务逻辑
        ResultEntity resultEntity = this.roleServiceApi.addRoleVindicatePermissions(roles, perids);
        return "admin/comm/success";
    }

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/editview")
    public String roleEditView(Integer id ,Model model){
        //获取所有的权限集合
        ResultEntity<List<Permission>> resultEntity = this.roleServiceApi.getPermissionsAll();
        //获取当前角色的权限集合  和角色信息
        ResultEntity<List<Integer>> currentPidsResult = this.roleServiceApi.getPermissionsByRole(id);
        ResultEntity<Roles> currentRoleResult = this.roleServiceApi.getRoleById(id);
        model.addAttribute("role",currentRoleResult.getData());
        model.addAttribute("perList",resultEntity.getData());
        model.addAttribute("pids",currentPidsResult.getData());
        return "admin/role/editview";
    }

    @Action(ActionType.UPDATE)
    @RequestMapping("/edit")
    public String roleEdit(Roles roles,Integer...perids){
        ResultEntity resultEntity = this.roleServiceApi.updateRoleVindicate(roles,perids);
        return "admin/comm/success";
    }

    @Action(ActionType.DELETE)
    @RequestMapping("/delete")
    @ResponseBody
    public Map roleDelete(Integer id){
        Map<String,Object> resultMaps = new HashMap<String,Object>();
        ResultEntity resultEntity = this.roleServiceApi.deleteRoleAndPermissions(id);
        if (ResultEntity.CODE_SUCCESS.equals(resultEntity.getCode())){
            resultMaps.put("code",200);
            resultMaps.put("msg","success");
        }else{
            resultMaps.put("code",301);
            resultMaps.put("msg","系统繁忙请稍后再试。");
        }
        return resultMaps;
    }
}
